/*
 * =====================================================================================
 *
 *       Filename:  naivebayes.h
 *
 *    Description:  Naive Bayes
 *
 *        Version:  1.0
 *        Created:  2009年07月29日 09时10分35秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Ying Wang (WY), ywang@nlpr.ia.ac.cn
 *        Company:  Institute of Automation, Chinese Academy of Sciences
 *
 * =====================================================================================
 */
#ifndef NAIVEBAYES_H
#define NAIVEBAYES_H
#include "ncmatrix.h"
#include "ncvector.h"
class NaiveBayes
{
public:
	NaiveBayes(const NCmatrix<double> &data);
public:
	inline NCvector<double> naiveBayesModel() const
	{
		return model;
	}
private:
	NCmatrix<double> m_data; //training data, each row is a training sample.
	int V; // vocabulary
	int N; // number of training sample
	NCvector<double> model;	
};

NaiveBayes::NaiveBayes( const NCmatrix<double> &data )
	: m_data(data)
	, V(m_data.column())
	, N(m_data.row())
	, model(V)
{
	int j,n;
	double tmp,sumt=0.;
	for( j=0; j<V; j++ )
	{
		for(tmp=0.; n=0; n<N; n++ )
		{
			tmp += m_data[n][j];
		}
		model[j] = tmp;
		sumt += tmp;
	}
	for( j=0; j<V; j++ )
	{
		model[j] /= sumt;
	}
}

#endif
